﻿namespace CatClient.Context
{
    using CatClient;
    using Newtonsoft.Json;
    using System;
    using System.Collections.Generic;
    using System.Reflection;
    using System.Runtime.CompilerServices;

    [Serializable]
    public class CatContext
    {
        private readonly Dictionary<string, string> _dict = new Dictionary<string, string>();

        private const string _catRootId = "X-Cat-RootId";

        private const string _catParentId = "X-Cat-ParentId";

        private const string _catChildId = "X-Cat-Id";

        public string CatRootId
        {
            get
            {
                return this["X-Cat-RootId"];
            }
            set
            {
                this["X-Cat-RootId"] = value;
            }
        }

        public string CatParentId
        {
            get
            {
                return this["X-Cat-ParentId"];
            }
            set
            {
                this["X-Cat-ParentId"] = value;
            }
        }

        public string CatChildId
        {
            get
            {
                return this["X-Cat-Id"];
            }
            set
            {
                this["X-Cat-Id"] = value;
            }
        }

        public string ContextName
        {
            get;
            set;
        }

        public string this[string key]
        {
            get
            {
                if (!this._dict.ContainsKey(key))
                {
                    return null;
                }
                return this._dict[key];
            }
            set
            {
                this._dict[key] = value;
            }
        }

        public CatContext()
        {
            this.ContextName = CatConfig.Config.Domain.Id;
        }

        [Obsolete("contextName直接取当前应用domain，不传入")]
        public CatContext(string contextName) : this()
        {
        }

        public IEnumerator<KeyValuePair<string, string>> GetEnumerator()
        {
            return ((IEnumerable<KeyValuePair<string, string>>)this._dict).GetEnumerator();
        }

        public override string ToString()
        {
            return JsonConvert.SerializeObject(this);
        }
    }
}

